<template>
  <div>
    <el-dialog
      title="生产订单领取及完成情况表"
      :visible.sync="isProducts"
      width="60%"
      :close-on-click-modal="false"
      @close="isProducts_close"
    >
      <el-table
        :data="moreList"
        stripe
        @selection-change="handleSelectionChang"
        style="width: 100%"
        border
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="serial_number" label="工种" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.job_type.job_type }}</span>
          </template>
        </el-table-column>
        <el-table-column label="领单人">
          <template slot-scope="scope">
            <span
              v-if="scope.row.is_epiboly == 1 || scope.row.employee"
              :style="{ color: scope.row.is_epiboly == 1 ? 'red' : '' }"
              >{{
                scope.row.is_epiboly == 1
                  ? "外包"
                  : scope.row.employee.employee_name
              }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="指派日期">
          <template slot-scope="scope">
            <span v-if="scope.row.is_epiboly == 1 || scope.row.employee">{{
              scope.row.is_epiboly == 1
                ? scope.row.epiboly_time
                : scope.row.appoint_time
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="draw_time" label="领单日期"> </el-table-column>
        <el-table-column prop="submit_time" label="完成日期"> </el-table-column>
        <el-table-column label="操作" width="230" fixed="right">
          <template slot-scope="scope">
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="确定外包该订单吗？"
              @confirm="set_waibaos(scope.row)"
            >
              <el-button
                slot="reference"
                type="text"
                style="margin-right: 10px"
                v-if="scope.row.draw_time == null && scope.row.is_epiboly == 2"
                >外包</el-button
              >
            </el-popconfirm>
            <el-button
              style="margin-right: 10px"
              @click="set_true_yuangong(scope.row)"
              type="text"
              v-if="scope.row.status == 1"
              >修改完成员工</el-button
            >
            <el-button
              style="margin-right: 10px"
              @click="set_yuangong(scope.row)"
              type="text"
              v-if="scope.row.status == 3 || scope.row.status == 2"
              >指派</el-button
            >

            <el-button
              style="margin-right: 10px"
              @click="setqx_yuangong(scope.row)"
              type="text"
              v-if="scope.row.draw_time"
              >改为未领取</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="add_dayinpush" type="primary" size="small"
          >添加到打印列表</el-button
        >
        <el-button @click="isProducts = false" type="primary" size="small"
          >确定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      :title="type"
      :visible.sync="isset"
      width="30%"
      :close-on-click-modal="false"
      v-if="moreLists.id && push_more.id"
    >
      <el-row
        style="
          height: 50px;
          line-height: 50px;
          display: flex;
          align-items: center;
        "
      >
        <el-col :span="3"> 订单ID </el-col>
        <el-col :span="21">
          {{ moreLists.id }}
        </el-col>
      </el-row>
      <el-row
        style="
          height: 50px;
          line-height: 50px;
          display: flex;
          align-items: center;
        "
      >
        <el-col :span="3"> 产品名 </el-col>
        <el-col :span="21">
          {{ moreLists.product.product_name }}
        </el-col>
      </el-row>
      <el-row
        style="
          height: 50px;
          line-height: 50px;
          display: flex;
          align-items: center;
        "
      >
        <el-col :span="3"> 订单型号 </el-col>
        <el-col :span="21">
          {{ moreLists.model_number.model_number }}
        </el-col>
      </el-row>
      <el-row
        style="
          height: 50px;
          line-height: 50px;
          display: flex;
          align-items: center;
        "
      >
        <el-col :span="3"> 当前工序 </el-col>
        <el-col :span="21">
          {{ push_more.job_type.job_type }}
        </el-col>
      </el-row>
      <el-row
        style="
          height: 50px;
          line-height: 50px;
          display: flex;
          align-items: center;
        "
      >
        <el-col :span="3"> 员工名称 </el-col>
        <el-col :span="21">
          <!-- 恒泰 -->
          <!-- <el-select v-model="push_prder.employee_user_id" placeholder="请选择员工" style="width: 100%">
                        <el-option v-for="(item, index) in pushCustomerList" :key="index" :label="item.user.employee_name"
                            :value="item.user_id">
                        </el-option>
                    </el-select> -->
          <!-- 暮轩 -->
          <el-select
            v-model="push_prder.employee_user_id"
            placeholder="请选择员工"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in pushCustomerList"
              :key="index"
              :label="item.employee.employee_name"
              :value="item.pivot.user_id"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isset = false">取消</el-button>
        <el-button @click="push_set" v-if="type == '指派'">确定</el-button>
        <el-button @click="push_true_set" v-if="type == '完成'">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserListByJobTypeId,
  assignOrderChild,
  epibolyOrderChild,
  cancelAssignOrderChild,
  getOrderChildListByOrderId,
  updateOrderChildEmployee,
  addOrderPrint,
} from "@/api/product/index";
export default {
  data() {
    return {
      type: "",
      pushCustomerList: [],
      moreLists: [],
      isProducts: false,
      isset: false,
      moreList: [],
      push_more: {},
      // 指派提交数据
      push_prder: {
        child_order_id: "",
        employee_user_id: "",
      },
      formatterTime(row, column) {
        let data = row[column.property];
        return /\d{4}-\d{1,2}-\d{1,2}/g.exec(data);
      },
    };
  },
  methods: {
    formatterTime(data) {
      if(data){
        return /\d{4}-\d{1,2}-\d{1,2}/g.exec(data).splice(0, 11)[0];
      }else{
        return "";
      }
     
    },
    dayin_2(ids) {
      let obj = {
        order_id: this.order_ids,
        order_child_ids: ids,
      };
      addOrderPrint(obj).then((res) => {
        if (res.code == 200) {
          this.$message.success("添加成功");
        }
      });
    },
    postw_drawOrderChildByEmployeeCardNumnber(user_id, order_child_id) {
      let obj = {
        order_child_id,
        user_id,
        parent_username: this.parent_username,
      };
      drawOrderChildByEmployeeCardNumnber(obj).then((res) => {
        if (res.code == 200) {
          this.getOrderChild(this.order_ids);
          this.dayin(this.order_ids, order_child_id);
        } else {
          this.$message.error("领单失败");
        }
      });
    },
    add_dayinpush() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("请选择子订单");
        return false;
      }
      let noling = [];
      let ids = [];
      for (var i = 0; i < this.multipleSelections.length; i++) {
        if (
          this.multipleSelections[i].employee == null &&
          this.multipleSelections[i].is_epiboly == 2 &&
          this.multipleSelections[i].is_appoint == 2
        ) {
          this.$message.error("所选订单有未指派的订单！请指派后在添加~");
          return false;
        }
        if (!this.multipleSelections[i].draw_time) {
          let obj = {
            user_id: this.multipleSelections[i].employee_user_id,
            order_child_id: this.multipleSelections[i].id,
          };
          // 没有领单的
          noling.push(obj);
        } else {
          // 有领单的
          ids.push(this.multipleSelections[i].id);
        }
      }
      // 有领单的
      if (ids.length > 0) {
        this.dayin_2(ids);
      }
      // 没有领单的
      if (noling.length > 0) {
        noling.forEach((item) => {
          this.postw_drawOrderChildByEmployeeCardNumnber(
            item.user_id,
            item.order_child_id
          );
        });
      }
    },
    // 获取对应子订单信息
    getOrderChild(id) {
      this.order_ids = id;
      getOrderChildListByOrderId({ order_id: id }).then((res) => {
        if (res.code == 200) {
          // this.moreLists=res.data
          this.moreList = res.data;
        }
      });
    },
    open_more(row) {
      this.model_number_idsss = row.model_number_id;
      this.moreLists = row;
      this.getOrderChild(row.id);
      this.isProducts = true;
    },
    handleSelectionChang(val) {
      if (val.length > 0) {
        this.multipleSelections = val;
        this.order_id = val[0].order_id;
        this.multipleSelection = [];
        val.forEach((item) => {
          let obj = {
            user_id: item.employee_user_id,
            order_child_id: item.id,
          };
          this.multipleSelection.push(obj);
        });
      } else {
        this.multipleSelection = [];
      }
    },
    isProducts_close() {
      this.order_ids = "";
    },
    // 修改完成时间
    push_true_set() {
      updateOrderChildEmployee(this.push_prder).then((res) => {
        this.$message.success("指派成功");
        this.isset = false;
        this.getOrderChild(this.moreLists.id);
        this.push_prder = {
          child_order_id: "",
          employee_user_id: "",
        };
        this.get_childList();
      });
    },
    // 指派提交
    push_set() {
      assignOrderChild(this.push_prder).then((res) => {
        this.$message.success("指派成功");
        this.isset = false;
        this.getOrderChild(this.moreLists.id);
        this.push_prder = {
          child_order_id: "",
          employee_user_id: "",
        };
        this.$emit("get_order");
      });
    },
    // 外包提交
    set_waibaos(row) {
      epibolyOrderChild({ child_order_id: row.id }).then((res) => {
        if (res.code == 200) {
          this.$message.success("外包成功");
          this.getOrderChild(this.moreLists.id);
          this.$emit("get_order");
        }
      });
    },
    set_true_yuangong(row) {
      this.push_more = row;
      this.type = "完成";
      this.push_prder.child_order_id = row.id;
      this.getUser(row.job_type.id).then((res) => {
        console.log(res);
        if (res == true) {
          this.isset = true;
        } else {
          this.$message.error("没有对应员工！");
        }
      });
    },
    set_yuangong(row) {
      this.type = "指派";
      this.push_more = row;
      this.push_prder.child_order_id = row.id;
      this.getUser(row.job_type.id).then((res) => {
        console.log(res);
        if (res == true) {
          this.isset = true;
        } else {
          this.$message.error("没有对应员工！");
        }
      });
    },
    //打开指派弹窗
    setqx_yuangong(row) {
      console.log(row);
      this.$confirm("此操作将取消该指派！ 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          cancelAssignOrderChild({ order_child_id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message.success("取消成功！");
              this.$emit("get_order");
              this.getOrderChild(this.moreLists.id);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改",
          });
        });
    },
    //获取指定工种员工列表
    async getUser(id) {
      var istrue;
      await getUserListByJobTypeId({ job_type_id: id }).then((res) => {
        if (res.code == 200) {
          this.pushCustomerList = res.data;
          console.log(this.pushCustomerList);
          istrue = true;
        } else {
          istrue = false;
        }
      });
      return istrue;
    },
    // 获取对应子订单信息
    getOrderChild(id) {
      this.order_ids = id;
      getOrderChildListByOrderId({ order_id: id }).then((res) => {
        if (res.code == 200) {
          // this.moreLists=res.data
          this.moreList = res.data;
        }
      });
    },
  },
};
</script>

<style></style>
